﻿using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation;
using THE_WIND_RISES.Common.Domans.Produce.Process;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Application.Commands.Week1.Process_CRUD
{
    /// <summary>
    /// 添加工序
    /// </summary>
    public class AddProcessHandler : IRequestHandler<AddProcessCommand, Result>
    {
        public AddProcessHandler(IBaseRepository<Process> processRepository, IBaseRepository<ProcessDetail> processDetailRepository)
        {
            ProcessRepository = processRepository;
            ProcessDetailRepository = processDetailRepository;
        }

        public IBaseRepository<Process> ProcessRepository { get; set; }
        public IBaseRepository<ProcessDetail> ProcessDetailRepository { get; set; }
        public async Task<Result> Handle(AddProcessCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddProcessSQL = "insert into Process values(@ProcessId,@ProcessName,@WorkCentre,@ProcessState,@ProcessAudit,@IsDelProcess,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete,@ProcessNum);";
            //var AddProcessSQL = $"insert into Process values({request.ProcessId},'{request.ProcessNum}','{request.ProcessName}','{request.WorkCentre}',{request.ProcessState},{request.ProcessAudit},{request.IsDelProcess},'{request.Handle}','{request.HandleContext}','{request.HandleTime}',{request.Pid},{request.IsDelete});";
            var AddProcessNum = ProcessRepository.Add(AddProcessSQL, new
            {
                ProcessId = request.ProcessId,
                ProcessNum=request.ProcessNum,
                ProcessName = request.ProcessName,
                WorkCentre = request.WorkCentre,
                ProcessState = request.ProcessState,
                ProcessAudit = request.ProcessAudit,
                IsDelProcess = request.IsDelProcess,
                Handle = request.Handle,
                HandleContext = request.HandleContext,
                HandleTime = request.HandleTime,
                Pid = request.Pid,
                IsDelete = request.IsDelete,
            });
            if (AddProcessNum > 0)
            {
                int AddProcessDetailNum = 0;
                foreach (var item in request.processDetails)
                {
                    var AddProcessDetailSql = "insert into ProcessDetail values(@MyProperty,@WarehouseADId,@ProcessDetailName,@ProcessDetailUnits,@ProcessDetailDosage,@UnitsPrice,@ProcessDetailState,@IsDelProcessDetail,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete,@ProcessDetailNum,@CreateTime,@CreateName,@AbstractData);";
                    AddProcessDetailNum = ProcessDetailRepository.Add(AddProcessDetailSql, new
                    {
                        MyProperty = 0,
                        WarehouseADId = request.ProcessNum,
                        ProcessDetailNum = item.ProcessDetailNum,
                        ProcessDetailName = item.ProcessDetailName,
                        ProcessDetailUnits = item.ProcessDetailUnits,
                        ProcessDetailDosage = item.ProcessDetailDosage,
                        UnitsPrice = item.UnitsPrice,
                        AbstractData = item.AbstractData,
                        CreateTime = item.CreateTime,
                        CreateName = item.CreateName,
                        ProcessDetailState = item.ProcessDetailState,
                        IsDelProcessDetail = item.IsDelProcessDetail,
                        Handle = request.Handle,
                        HandleContext = request.HandleContext,
                        HandleTime = request.HandleTime,
                        Pid = request.Pid,
                        IsDelete = request.IsDelete,
                    });
                }
                if (AddProcessDetailNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            else
            {
                return result;
            }
        }
    }
}
